//
//  QBDefine.h
//  Qiaobao
//
//  Created by JK.PENG on 13-3-13.
//  Copyright (c) 2013年 qianwang365. All rights reserved.
//

#ifndef Qiaobao_QBDefine_h
#define Qiaobao_QBDefine_h


#import "Util.h"

#define URLSTRINGFORMAT(STRA,STRB)                  [NSString stringWithFormat:@"%@/%@",STRA,STRB]

#define IOS_VERSION                                 [[[UIDevice currentDevice] systemVersion] doubleValue]

/****************************************UI布局宏*****************************************/
#define _CGP(x, y)                                   CGPointMake(x, y)
#define _CGS(w, h)                                   CGSizeMake(w, h)
#define _CGR(x, y, w, h)                             CGRectMake(x, y, w, h)
#define _CGC(__Frame__)                              CGPointMake(CGRectGetMidX(__Frame__),CGRectGetMidY(__Frame__))
#define _RGB_A(r, g, b, a)                          [UIColor colorWithRed:r/256.0f green:g/256.0f blue:b/256.0f alpha:a]

#define _NIL_POINT(__class__,__pname__)\
        __class__ *__pname__ = nil;\

//UILabel
#define _CREATE_LABEL(__label__, __frame__, __fontsize__)\
        _CREATE_LABEL_ALIGN(__label__, __frame__, ALIGN_LEFT, __fontsize__)

#define _CREATE_LABEL_ALIGN(__label__, __frame__, __align__, __fontsize__)\
{\
    __label__ = _ALLOC_OBJ_WITHFRAME_(UILabel, __frame__);\
    [__label__ setBackgroundColor:[UIColor clearColor]];\
    [__label__ setTextAlignment:__align__];\
    [__label__ setFont:FONT_TITLE(__fontsize__)];\
}

#define _CREATE_LABEL_ALIGN_BOLDFONT(__label__, __frame__, __align__, __boldfontsize__)\
{\
    __label__ = _ALLOC_OBJ_WITHFRAME_(UILabel, __frame__);\
    [__label__ setBackgroundColor:[UIColor clearColor]];\
    [__label__ setTextAlignment:__align__];\
    [__label__ setFont:BOLDFONT_TITLE(__boldfontsize__)];\
}


#define _CREATE_LABEL_ALIGN_AND_ADDSUBVIEW(__label__, __frame__, __align__, __font__,__superView__)\
{\
    __label__ = _ALLOC_OBJ_WITHFRAME_(UILabel, __frame__);\
    [__label__ setBackgroundColor:[UIColor clearColor]];\
    [__label__ setTextAlignment:__align__];\
    [__label__ setFont:__font__];\
    [__superView__ addSubview:__label__];\
}

#define _CREATE_LABEL_AND_ADDSUBVIEW(__label__, __frame__, __align__, __font__,__superView__,__text__,__color__)\
{\
__label__ = _ALLOC_OBJ_WITHFRAME_(UILabel, __frame__);\
[__label__ setBackgroundColor:[UIColor clearColor]];\
[__label__ setTextAlignment:__align__];\
[__label__ setFont:__font__];\
[__superView__ addSubview:__label__];\
[__label__ setText:__text__];\
[__label__ setTextColor:[QBUtil getColor:__color__]];\
}

//UIImageView
#define _CREATE_UIIMAGEVIEW_NANME(__imageview__, __imagename__)\
{\
    __imageview__ = [[UIImageView alloc]initWithImage:IMGFROMBUNDLE(__imagename__)];\
}

#define _CREATE_UIIMAGEVIEW_NANME_POINT_AND_ADDSUBVIEW(__imageview__, __imagename__, __superView__,__centerPoint__)\
{\
    UIImage *img = IMGFROMBUNDLE(__imagename__);\
    CGRect frame = CGRectMake(__centerPoint__.x - img.size.width/2  , __centerPoint__.y -img.size.height/2  , img.size.width , img.size.height);\
    _CREATE_UIIMAGEVIEW_NANME_FRAME_AND_ADDSUBVIEW(__imageview__, __imagename__, __superView__, frame)\
}\

#define _CREATE_UIIMAGEVIEW_NANME_FRAME_AND_ADDSUBVIEW(__imageview__, __imagename__, __superView__, __frame__)\
{\
    UIImage *img = IMGFROMBUNDLE(__imagename__);\
    __imageview__ = [[UIImageView alloc]initWithImage:img];\
    [__superView__ addSubview:__imageview__];\
    [__imageview__ setFrame:__frame__];\
}\

#define _CREATE_UIIMAGEVIEW_URLNANME_FRAME_AND_ADDSUBVIEW(__imageview__, __imgurl__, __superView__, __frame__, __placeholdername__)\
{\
    __imageview__ = [[UIImageView alloc]initWithFrame:__frame__];\
    [__imageview__ setImageWithURLString:__imgurl__ placeholderImage:IMGFROMBUNDLE(__placeholdername__)];\
    [__imageview__ setBackgroundColor:[UIColor whiteColor]];\
    [__superView__ addSubview:__imageview__];\
}\

#define _CREATE_UIIMAGEVIEW(__imageview__, __frame__, __imagename__)\
{\
    _CREATE_UIIMAGEVIEW_IMAGE(__imageview__, __frame__, IMGFROMBUNDLE(__imagename__));\
}

#define _CREATE_UIIMAGEVIEW_IMAGE(__imageview__, __frame__, __image__)\
{\
    __imageview__       = _ALLOC_OBJ_WITHFRAME_(UIImageView, __frame__);\
    __imageview__.image = __image__;\
}

//UIView
#define _CREATE_VIEW(__view__, __frame__, __bgcolor__)\
{\
    __view__ = _ALLOC_OBJ_WITHFRAME_(UIView, __frame__);\
    [__view__ setBackgroundColor:__bgcolor__];\
}

//UIButton
#define _CREATE_ALLOC_BUTTON(__btn__, __frame__, __selector__)\
{\
__btn__       = _ALLOC_OBJ_WITHFRAME_(UIButton, __frame__);\
[__btn__ addTarget:self action:__selector__ forControlEvents:UIControlEventTouchUpInside];\
}

#define _CREATE_BUTTON(__btn__, __frame__, __title__, __titlefontsize__, __selector__)\
{\
    __btn__       = [UIButton buttonWithType:UIButtonTypeCustom];\
    __btn__.frame = __frame__;\
    [__btn__ addTarget:self action:__selector__ forControlEvents:UIControlEventTouchUpInside];\
    [__btn__ setTitle:__title__ forState:UIControlStateNormal];\
    [__btn__.titleLabel setFont:FONT_TITLE(__titlefontsize__)];\
    [__btn__ retain];\
}

//UIScrollView
#define _CREATE_SCROLLVIEW(__scrollview__, __frame__)\
{\
    __scrollview__ = _ALLOC_OBJ_WITHFRAME_(UIScrollView, __frame__);\
    [__scrollview__ setBackgroundColor:[UIColor clearColor]];\
    [__scrollview__ setShowsHorizontalScrollIndicator:NO];\
    [__scrollview__ setShowsVerticalScrollIndicator:NO];\
}

//tableView
#define _CREATE_TABLEVIEW(__table__, __frame__, __tabletype__)\
{\
    __table__ = [[UITableView alloc]initWithFrame:__frame__ style:__tabletype__];\
    __table__.delegate         = self;\
    __table__.dataSource       = self;\
    [__table__ setBackgroundColor:[UIColor clearColor]];\
}

#define _PROPERTY_NONATOMIC_RETAIN(__class__, __property__)\
@property (nonatomic, retain)__class__  *__property__;

#define _PROPERTY_NONATOMIC_READONLY(__class__, __property__)\
@property(nonatomic, readonly)__class__   __property__;

#define _PROPERTY_NONATOMIC_ASSIGN(__class__, __property__)\
@property (nonatomic, assign)__class__  __property__;

#define _PROPERTY_NONATOMIC_COPY(__class__, __property__)\
@property (nonatomic, copy)__class__  *__property__;

//****************************************GETTER****************************************/
#define _GETTER_BEGIN(__class__, __property__) \
- (__class__*)__property__ \
{\
if(!_##__property__)\
{\


#define _GETTER_ALLOC_BEGIN(__class__, __property__) \
- (__class__*)__property__ \
{\
    if(!_##__property__)\
    {\
        _##__property__ = _ALLOC_OBJ_(__class__);

#define _GETTER_ALLOC_FRAME_BEGIN(__class__, __property__, __frame__) \
- (__class__*)__property__ \
{\
if(!_##__property__)\
{\
_##__property__ = _ALLOC_OBJ_WITHFRAME_(__class__, __frame__);


#define _GETTER_END(__property__) \
    }\
    return _##__property__;\
}
//****************************************getter宏****************************************/

//****************************************setter宏****************************************/
#define _SETTER_RETAIN(__property__)\
{\
    if(_##__property__ != __property__)\
    {\
        RELEASE_SAFELY(_##__property__);\
        _##__property__ = [__property__ retain];\
    }\
}

#define _SETTER_COPY(__property__)\
{\
    if(_##__property__ != __property__)\
    {\
        RELEASE_SAFELY(_##__property__);\
        _##__property__ = [__property__ copy];\
    }\
}
//=========setter宏============

#ifndef _ALLOC_OBJ_
#define _ALLOC_OBJ_(__obj_class__) \
        [[__obj_class__ alloc]init]
#endif

#ifndef _ALLOC_OBJ_AUTORELEASE_
#define _ALLOC_OBJ_AUTORELEASE_(__obj_class__) \
        [[[__obj_class__ alloc]init] autorelease]
#endif

#ifndef _ALLOC_OBJ_WITHFRAME_
#define _ALLOC_OBJ_WITHFRAME_(__obj_class__, __frame__) \
        [[__obj_class__ alloc]initWithFrame:__frame__]
#endif

#ifndef _ALLOC_OBJ_WITHFRAME_AUTORELEASE_
#define _ALLOC_OBJ_WITHFRAME_AUTORELEASE_(__obj_class__, __frame__) \
        [[[__obj_class__ alloc]initWithFrame:__frame__] autorelease]
#endif

#ifndef _ALLOC_OBJ_WITHDICT_
#define _ALLOC_OBJ_WITHDICT_(__obj_class__, __dict__) \
        [[__obj_class__ alloc]initWithDict:__dict__]
#endif

#ifndef _CLEAR_BACKGROUND_COLOR_
#define _CLEAR_BACKGROUND_COLOR_(_view_) \
        do {[_view_ setBackgroundColor:[UIColor clearColor]];} while(0)
#endif

#ifndef _SET_FRAME_
#define _SET_FRAME_(_view_,_frame_)\
        do {[_view_ setFrame:_frame_];}while(0)
#endif

//从userDefault中获取值
#ifndef getObjectFromUserDefault
#define getObjectFromUserDefault(__key__) \
        [[NSUserDefaults standardUserDefaults] objectForKey:__key__]
#endif

//setuserDefault值
#ifndef setObjectToUserDefault
#define setObjectToUserDefault(__key__, __val__) \
        [[NSUserDefaults standardUserDefaults] setObject:__val__ forKey:__key__];
#endif

//从userDeufalt中移除值
#ifndef removeObjectFromUserDefault
#define removeObjectFromUserDefault(__key__) \
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:__key__];
#endif

//字典赋值
#ifndef SET_PARAM
#define SET_PARAM(__value__, __key__, __parms__) \
if (nil!=__value__) {\
[__parms__ setObject:__value__ forKey:__key__];\
}
#endif

//字典是否有K-V
#ifndef isDictWithCountMoreThan0

#define isDictWithCountMoreThan0(__dict__) \
(__dict__!=nil && \
[__dict__ isKindOfClass:[NSDictionary class] ] && \
__dict__.count>0)

#endif

#define isDictContainKey(__dic__, __key__)\
        !(nil == [__dic__ objectForKey:__key__])

//获取字典中的错误信息
#define getErrMsg(dict)     [dict objectForKey:KEY_ERRORMSG]
#define getData(dict)       [dict objectForKey:KEY_RETURNDATA]

//数组是否有
#ifndef isArrayWithCountMoreThan0

#define isArrayWithCountMoreThan0(__array__) \
(__array__!=nil && \
[__array__ isKindOfClass:[NSArray class] ] && \
__array__.count>0)

#endif

#define _SIMPLE_ALERT_S_C_(__title, __message, __delegate)\
{\
UIAlertView* __alert__ = [[UIAlertView alloc] initWithTitle:__title message:__message delegate:__delegate cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];\
[__alert__ show];\
[__alert__ release];\
}

#define _SIMPLE_ALERT_(__title, __message, __btnTitle)\
{\
UIAlertView* __alert__ = [[UIAlertView alloc] initWithTitle:__title message:__message delegate:nil cancelButtonTitle:__btnTitle otherButtonTitles:nil,nil];\
[__alert__ show];\
[__alert__ release];\
}


//lineBreakMode
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
#define NSUITextAlignment UITextAlignment
#define NSUILineBreakMode UILineBreakMode

#define NSUILineBreakModeWordWrap             UILineBreakModeWordWrap
#define NSUILineBreakModeCharacterWrap        UILineBreakModeCharacterWrap
#define NSUILineBreakModeClip                 UILineBreakModeClip
#define NSUILineBreakModeHeadTruncation       UILineBreakModeHeadTruncation
#define NSUILineBreakModeTailTruncation       UILineBreakModeTailTruncation
#define NSUILineBreakModeMiddleTruncation     UILineBreakModeMiddleTruncation
#else
#define NSUITextAlignment NSTextAlignment
#define NSUILineBreakMode NSLineBreakMode

#define NSUILineBreakModeWordWrap             NSLineBreakByWordWrapping
#define NSUILineBreakModeCharacterWrap        NSLineBreakByCharWrapping
#define NSUILineBreakModeClip                 NSLineBreakByClipping
#define NSUILineBreakModeHeadTruncation       NSLineBreakByTruncatingHead
#define NSUILineBreakModeTailTruncation       NSLineBreakByTruncatingTail
#define NSUILineBreakModeMiddleTruncation     NSLineBreakByTruncatingMiddle
#endif

//rgb颜色
#define COLOR_RGB(r,g,b)                        [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0]
#define COLOR_RGBA(r,g,b,a)                     [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
//请求头
#define APPLICATION_NAME                    @"QianbaoIOS"
#define kAgient                             [NSString stringWithFormat:@"%@/%@",APPLICATION_NAME,[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey]]
#define APP_NAME                            [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
#define APP_VERSION                         [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

//对齐方式
#define ALIGN_LEFT      [QBUtil getAlign:ALIGNTYPE_LEFT]
#define ALIGN_CENTER    [QBUtil getAlign:ALIGNTYPE_CENTER]
#define ALIGN_RIGHT     [QBUtil getAlign:ALIGNTYPE_RIGHT]

//安全释放
#define RELEASE_SAFELY(__POINTER) do{ [__POINTER release]; __POINTER = nil; } while(0)
#define INVALIDATE_TIMER(__TIMER) do{ [__TIMER invalidate];[__TIMER release]; __TIMER = nil; } while(0)

//图片资源获取
#define IMGFROMBUNDLE( X )	 [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:X ofType:@"" ]]
#define IMGNAMED( X )	     [UIImage imageNamed:X]

//日志打印
#ifdef DEBUG
#define DBLog(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define DBLog(xx, ...)  ((void)0)
#endif

//屏幕尺寸判断
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

//定义字号
#define FONT_TITLE(X)           [UIFont systemFontOfSize:X]
#define BOLDFONT_TITLE(X)       [UIFont boldSystemFontOfSize:X]


//解析字典
#define PUGetElemForKeyFromDict(__key, __dict)   [Util getElementForKey:__key fromDict:__dict]
#define PUGetObjFromDict(__key, __dict, __class) [Util getElementForKey:__key fromDict:__dict forClass:__class]

#define PUGetObjFromDictWithCharaterSet(__key, __dict, __characterSet) \
[Util getElementForKey:__key fromDict:__dict character:__characterSet]

//UI 动态布局
#define GetOriginX(UI)              CGRectGetMinX(UI.frame)
#define GetOriginY(UI)              CGRectGetMinY(UI.frame)
#define GetWidth(UI)                CGRectGetWidth(UI.frame)
#define GetHeight(UI)               CGRectGetHeight(UI.frame)
#define OffSetX(UI)                 CGRectGetMaxX(UI.frame)
#define OffSetY(UI)                 CGRectGetMaxY(UI.frame)



//创建单例
#define _SINGLETON_FOR_CLASS(classname) \
\
+ (classname*)shareInstance \
{ \
static dispatch_once_t pred = 0; \
__strong static classname* _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = [[self alloc] init]; \
}); \
return _sharedObject;\
}


//presentModal
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
#define _PRESENT_VC(VC,toVC,BL) [VC presentModalViewController:toVC animated:BL]
#define _DISSMIS_VC(VC,BL)      [VC dismissModalViewControllerAnimated:BL]
#else
#define _PRESENT_VC(VC,toVC,BL) [VC presentViewController:toVC animated:BL completion:nil]
#define _DISSMIS_VC(VC,BL)      [VC dismissViewControllerAnimated:BL completion:nil]
#endif

#endif
